<?php
namespace app\teacher\controller;

use app\teacher\controller\PublicController;
use app\common\logic\admin\Relation;
use app\common\logic\admin\Appuser;
use app\common\model\AppUserModel;
use app\common\model\RelationStudentModel;
use app\common\model\RelationClassModel;
//班级管理
class RelationController extends PublicController
{
    private $Relation;
    private $Appuser;
    public function __construct(){
        parent::__construct();
        $this->check();
        $this->install();
        $this->Relation = new Relation();
        $this->Appuser = new Appuser();
    }

    /**
     * 行政班
     */
    public function index_1()
    {
        $param = $this->request->param();
        $param['status'] = 1;
        $data = $this->Relation->getList($this->user_id,$param);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch('teacher@:relation/index_1');
    }
    /**
     * [主题班]
     * @return [type] [description]
     */
    public function index_2(){
        $param = $this->request->param();
        $param['status'] = 2;
        $data = $this->Relation->getList($this->user_id,$param);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch('teacher@:relation/index_2');

    }
    /**
     * [小组]
     * @return [type] [description]
     */
    public function index_3(){
        $param = $this->request->param();
        $param['status'] = 3;
        $data = $this->Relation->getList($this->user_id,$param);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch('teacher@:relation/index_3');
    }
  
    /**
     * [添加]
     */
    public function add(){
        $data_grad = $this->Appuser->grad($this->shcool_id)->toArray();
        /*foreach ($data_grad as $key => $value) {
           $data_grad[$key]['classes'] =  $this->Appuser->classes($this->shcool_id,$value['grad'])->toArray();
           foreach ($data_grad[$key]['classes'] as $k => $val) {
               $data_grad[$key]['classes'][$k]['user'] = $this->Appuser->user($this->shcool_id,$value['grad'],$val['classes'])->toArray();
           }
        }*/
        $this->assign('data_grad',$data_grad);
        return $this->fetch('teacher@:relation/add');
    }

    /**
     * [生成班级]
     * @param [type] $[status] [1行政班 2主题班 3小组]
     */
    public function add_1(){
    	$param = $this->request->param();
    	$ids = explode('-', $param['ids']);
    	$data = AppUserModel::where('id','in',$ids)->field('id,realName')->select();
    	$this->assign('data',$data);
    	$this->assign('status',$param['status']);
    	return $this->fetch('teacher@:relation/add_1');
    }
    
    /**
     * [提交]
     * @return [type] [description]
     */
    public function toAdd(){
    	$param = $this->request->param();
    	$class_id = $this->Relation->createClass($param['class_name'],$param['status'],$this->user_id);
    	$teacher_id = $this->Relation->teacher($class_id,$this->user_id,$param['status']);
    	$user_ids = explode('-', $param['vals']);
    	$rs = $this->Relation->student($class_id,$user_ids,$param['status']);
    	if($rs){
    		switch ($param['status']) {
    			case '1':
    				$this->success('成功','index_1');
    				break;
    			case '2':
    				$this->success('成功','index_2');
    				break;
    			default:
    				# code...
    				break;
    		}
    		
    	}else{
    		$this->error('失败');
    	}
    }


    /**
     * [从班级中选择成员生成小组]
     * @return [type] [description]
     */
    public function group(){
        $param = $this->request->param();
        $data = RelationStudentModel::where('class_id',$param['class_id'])->select();
        $this->assign('data',$data);
        return $this->fetch('teacher@:relation/group');
    }

    /**
     * [从班级中选择成员生成小组]
     * @return [type] [description]
     */
    public function group_1(){
        $param = $this->request->param();
        $ids = explode('-', $param['ids']);
        $data = AppUserModel::where('id','in',$ids)->field('id,realName')->select();
        $this->assign('data',$data);
        $this->assign('status',$param['status']);
        return $this->fetch('teacher@:relation/group_1');
    }

    /**
     * [提交]
     * @return [type] [description]
     */
    public function toGroup(){
        $param = $this->request->param();
        //创建班级
        $class_id = $this->Relation->createClass($param['class_name'],3,$this->user_id,$param['p_class_id']);
        //绑定老师关系
        $teacher_id = $this->Relation->teacher($class_id,$this->user_id,3);
        //绑定学生关系
        $user_ids = explode('-', $param['vals']);
        $rs = $this->Relation->student($class_id,$user_ids,3,0);
        //更新学生组长状态
        RelationStudentModel::where('class_id',$class_id)->where('user_id',$param['user_id'])->update(['level'=>1]);
        if($rs){
            $this->success('成功');
            
        }else{
            $this->error('失败');
        }
    }
    /**
     * [获取小组下的学生列表]
     * @return [type] [description]
     */
    public function getGroupLists(){
        $param = $this->request->param();
        $data = $this->Relation->getStulist($param,10);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch('teacher@:relation/group_list');
    }
    /**
     * [设置组长]
     * @return [type] [description]
     */
    public function setlevel(){
        $param = $this->request->param();
        RelationStudentModel::where('class_id',$param['class_id'])->update(['level'=>0]);
        $rs = RelationStudentModel::where('student_id',$param['student_id'])->update(['level'=>1]);
        if($rs){
            $this->success('成功');
            
        }else{
            $this->error('失败');
        }

    }
    /**
     * [获取班组下的学生列表]
     * @return [type] [description]
     */
    public function getLists(){
    	$param = $this->request->param();
    	$data = $this->Relation->getStulist($param,10);
    	$this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch('teacher@:relation/list');
    }
    /**
     * [从班组中移除]
     * @return [type] [description]
     */
    public function remove(){
    	$param = $this->request->param();
    	$rs = RelationStudentModel::where('student_id',$param['student_id'])->delete();
    	if($rs){
    		$this->success('成功');
    	}else{
    		$this->error('失败');
    	}
    }
    /**
     * [单独添加学生到班级]
     */
    public function addStudent(){
    	$param = $this->request->param();
    	$data = AppUserModel::where('phone',$param['phone'])->field('id,role')->find();
    	if(empty($data) || $data['role'] != 1){
    		$this->error('没有该学生');die;
    	}
    	$status = RelationClassModel::where('class_id',$param['class_id'])->value('status');
    	$user_ids[] = $data['id'];
        if($status == 3){
            $rs = $this->Relation->student($param['class_id'],$user_ids,$status,0);
        }else{
            $rs = $this->Relation->student($param['class_id'],$user_ids,$status);
        }
    	

    	if($rs){
    		$this->success('成功');
    	}else{
    		$this->error('失败');
    	}
    }
    /**
     * [课题记录]
     * @return [type] [description]
     */
     public function mytask(){
        $param = $this->request->param();
        $data = $this->Relation->mytask($param);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch('teacher@:relation/mytask');

    }
	/**
	 * 获取班级
	 */
	public function getClasses(){
		$param = $this->request->param();
		$data =  $this->Appuser->classes($this->shcool_id,$param['grad'])->toArray();
		$this->success('','',$data);
	}
	/**
	 * [获取学生列表]
	 * @return [type] [description]
	 */
	public function getData(){
		$param = $this->request->param();
		$data =  $this->Appuser->user($this->shcool_id,$param['grad'],$param['classes'])->toArray();
		$this->success('','',$data);
	}
}
